package org.neuedu.ssmp.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import lombok.extern.slf4j.Slf4j;
import org.neuedu.ssmp.crm.entity.Chances;
import org.neuedu.ssmp.crm.entity.CustomerContact;
import org.neuedu.ssmp.crm.entity.vo.ResponseEntity;
import org.neuedu.ssmp.crm.mapper.CustomerContactMapper;
import org.neuedu.ssmp.crm.service.ICustomerContactService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author MoonHolder
 * @since 2019-12-09
 */
@Slf4j
@Service
public class CustomerContactServiceImpl extends ServiceImpl<CustomerContactMapper, CustomerContact> implements ICustomerContactService {

    @Override
    public IPage<CustomerContact> contactPage(int pageIndex, int pageSize, int customerId) {
        return new LambdaQueryChainWrapper<>(baseMapper).eq(CustomerContact::getCustomerId,customerId).eq(CustomerContact::getIsDel,0).page(new Page<>(pageIndex,pageSize));
    }

    @Override
    public ResponseEntity add(CustomerContact contact) {
        contact.setIsDel(0);
        log.info(contact.toString());
        if (baseMapper.insert(contact) != 0) {
            return ResponseEntity.ok("新建联系人成功！");
        }
        return ResponseEntity.error("新建联系人失败！");
    }

    @Override
    public ResponseEntity del(int id) {
        CustomerContact contact = CustomerContact.builder().id(id).isDel(1).build();
        if (baseMapper.updateById(contact) != 0){
            return ResponseEntity.ok("删除联系人成功！");
        }
        return ResponseEntity.error("删除联系人失败！");
    }

    @Override
    public ResponseEntity edit(CustomerContact contact) {
        if (baseMapper.updateById(contact) != 0){
            return ResponseEntity.ok("修改联系人成功！");
        }
        return ResponseEntity.error("修改联系人失败！");
    }
}
